package com.example.meituan.activity.user;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.meituan.R;
import com.example.meituan.activity.bean.AdminBean;
import com.example.meituan.activity.bean.BusinessBean;
import com.example.meituan.activity.bean.OrderBean;
import com.example.meituan.activity.bean.ShopBean;
import com.example.meituan.activity.bean.ShopCarBean;
import com.example.meituan.activity.man.StoreActivity;
import com.example.meituan.activity.man.frament.ManageHomeFragment;
import com.example.meituan.activity.user.adapter.ProductAdapter;
import com.example.meituan.activity.user.adapter.ShopCarAdapter;
import com.example.meituan.dao.AdminDao;
import com.example.meituan.dao.OrderDao;
import com.example.meituan.dao.ShopCarDao;
import com.example.meituan.dao.ShopDao;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ShopCarActivity extends AppCompatActivity implements  OnShopItemClickListener {

    String userId;
    ShopCarAdapter adapter;
    List<ShopCarBean> shopCarBeans;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_shop_car);
        shopCarBeans=new ArrayList<>();
        AdminBean currentUser= AdminDao.getUserInfo(this);
        userId=currentUser.getUserId();

        List<ShopCarBean> shopCarBeans= ShopCarDao.findByUserId(userId);
        androidx.recyclerview.widget.RecyclerView recyclerView=findViewById(R.id.shop_car_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        if (shopCarBeans == null || shopCarBeans.isEmpty()) {
            Toast.makeText(this, "没有数据显示", Toast.LENGTH_SHORT).show();
            recyclerView.setAdapter(null);
        } else {
            adapter = new ShopCarAdapter(shopCarBeans,  this); ;
            recyclerView.setAdapter(adapter);
        }

        TextView back=findViewById(R.id.shop_car_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();//返回功能
                //finish();
            }
        });

        TextView num=findViewById(R.id.shop_car_num);
        num.setText("购物车("+ShopCarDao.CountByUserId(userId)+")");
        androidx.appcompat.widget.SearchView searchView=findViewById(R.id.shop_car_listView_search);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                List<ShopCarBean> shopCarBeans= ShopCarDao.findAllByUserIdName(userId,query);
                androidx.recyclerview.widget.RecyclerView recyclerView=findViewById(R.id.shop_car_recyclerView);
                if (shopCarBeans == null || shopCarBeans.isEmpty()) {
                    Toast.makeText(ShopCarActivity.this, "没有数据显示", Toast.LENGTH_SHORT).show();
                    recyclerView.setAdapter(null);
                } else {
                    adapter = new ShopCarAdapter(shopCarBeans,  ShopCarActivity.this); ;
                    recyclerView.setAdapter(adapter);
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        TextView manage=findViewById(R.id.shop_car_manage);
        manage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (shopCarBeans == null || shopCarBeans.isEmpty()) {
                    Toast.makeText(ShopCarActivity.this, "购物车为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                AlertDialog.Builder builder = new AlertDialog.Builder(ShopCarActivity.this);
                builder.setTitle("确定要删除这些商品吗？");

                builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 使用线程池或协程来执行数据库操作
                        new Thread(() -> {
                            boolean allDeleted = shopCarBeans.stream().noneMatch(item -> ShopCarDao.delete(item.getsId()) != 1);

                            // 回到主线程更新UI
                            runOnUiThread(() -> {
                                if (allDeleted) {
                                    Toast.makeText(ShopCarActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(ShopCarActivity.this, "部分商品删除失败", Toast.LENGTH_SHORT).show();
                                }
                            });

                            dialog.dismiss();
                        }).start();
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                // 显示对话框
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });

    }

    @Override
    public void onShopItemClick(ShopCarBean shopCarBean) {
        ShopBean shopBean= ShopDao.getShopById(shopCarBean.getsShopId());
        Intent intent=new Intent(ShopCarActivity.this, ProductDetialsActivity.class);
        intent.putExtra("shopBean", shopBean);
        startActivity(intent);
    }

    @Override
    public void toStore(ShopCarBean shopCarBean) {
        ShopBean shopBean=ShopDao.getShopById(shopCarBean.getsShopId());
        BusinessBean businessBean= AdminDao.getBusiness(shopBean.getBusinessId());
        Intent intent=new Intent(ShopCarActivity.this, StoreActivity.class);
        intent.putExtra("businessBean",  businessBean.getsId());
        startActivity(intent);
    }

    @Override
    public void onCheckedChanged(ShopCarBean shopCarBean, boolean isChecked) {
        if(isChecked){
            shopCarBeans.add(shopCarBean);
        }
    }
}